package com.ruoyi.fjtseac.service
import com.baomidou.mybatisplus.core.conditions.Wrapper
import com.ruoyi.common.constant.HttpStatus

import com.ruoyi.fjtseac.domain.dto.ApplicationFormDto
import com.ruoyi.fjtseac.domain.dto.VolunteerDto
import com.ruoyi.fjtseac.domain.po.ApplicationForm
import com.ruoyi.fjtseac.domain.po.Volunteer
import com.ruoyi.fjtseac.mapper.ApplicationFormMapper
import com.ruoyi.fjtseac.mapper.VolunteerMapper
import com.ruoyi.fjtseac.service.impl.VolunteerServiceImpl

import javax.validation.Validator
import spock.lang.Specification
import spock.lang.Unroll

import java.sql.Date
import java.time.LocalDate

class VolunteerServiceTest extends Specification{
    def volunteerMapper = Mock(VolunteerMapper)
    def applicationFormMapper =Mock(ApplicationFormMapper)
    def validator = Mock(Validator)
    def volunteerImpl = new VolunteerServiceImpl(volunteerMapper:volunteerMapper,applicationFormMapper: applicationFormMapper,validator: validator)




    @Unroll
    def "insert volunteer"(){
        given:
        def volunteerDto = new VolunteerDto(volunteerId:32010,name: "jiu",phone: "15358042006",gender: "男",createBy: "admin",joinTime: Date.valueOf(LocalDate.now()))

        volunteerMapper.selectCount(_ as Wrapper<Volunteer>) >> res1
        volunteerMapper.insert(_ as Volunteer) >> res2


        when:
        def response1 = volunteerImpl.insertVolunteer(volunteerDto,"admin")


        then:
        with(response1){
            code == res3
        }

        where:
        res1 | res2 || res3
        0    | 1    || HttpStatus.SUCCESS
        1    | 1    || HttpStatus.ERROR

    }

    @Unroll
    def "agree to be volunteer"(){

        given:
        def applicationFormDto = new ApplicationFormDto(name: "jiu",phone: "15358042006",gender: "男")

        volunteerMapper.selectCount(_ as Wrapper<Volunteer>)>>num
        volunteerMapper.insertVolunteer(_ as Volunteer)>>res_1
        applicationFormMapper.updateById(_ as ApplicationForm)>>res_2

        when:
        def ans = volunteerImpl.agreeVolunteer(applicationFormDto,"admin")

        then:
        with(ans){
            code == result
        }

        where:
        num|res_1|res_2||result
        0  |1    |1    ||HttpStatus.SUCCESS
        1  |1    |1    ||HttpStatus.ERROR

    }



    @Unroll
    def "select volunteerList "(){
        given:
        volunteerMapper.selectVolunteerList(_ as Volunteer)>>res

        when:
        def ans = volunteerImpl.selectVolunteerList(volunterrDto)

        then:
        with(ans){
            ans == res
        }

        where:
        volunterrDto | res
        new VolunteerDto(volunteerId: 1)|[new Volunteer(volunteerId: 1,name: "aa")]
        new VolunteerDto(name: "cc")    |[new Volunteer(volunteerId: 2,name: "cc"),new Volunteer(volunteerId: 3,name:"cc")]
    }

    @Unroll
    def "import volunteer excel_1"(){
        given:
        volunteerMapper.selectVolunteerByVolunteerPhone(_ as String)>>volunteer
        validator.validate(_ as Volunteer)>>list

        when:
       def ans = volunteerImpl.importVolunteer(volunteerList,false,"admin")

        then:
        with(ans){
            ans == result
        }

        where:
        volunteerList   |   volunteer   | list |result
        [new Volunteer(name: "找",phone: "123",gender: "男")] | null | [] |"恭喜您，数据已全部导入成功！共 1 条，数据如下：<br/>1、账号 找 导入成功"
    }

    @Unroll
    def "import volunteer excel_2"(){
        given:
        volunteerMapper.selectVolunteerByVolunteerPhone(_ as String)>>volunteer
        validator.validate(_ as Volunteer)>>list

        when:
        volunteerImpl.importVolunteer(volunteerList,false,"admin")

        then:
        def expection = thrown(Exception)
        expection.message == result

        where:
        volunteerList   |   volunteer |list  |result
                    null| null        |  []  |  "导入用户数据不能为空！"
        [new Volunteer(name: "找",phone: "123",gender: "男")] | null |[[]]|"很抱歉，数据导入失败！共 1 条数据/格式不正确，错误如下：<br/>1、账号 找 导入失败：java.util.ArrayList cannot be cast to javax.validation.ConstraintViolation"
    }

}
